package st.business.tepth.business.goodsreceive;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.MaterialWaitApplyBean;
import st.business.tepth.business.bean.post.IDBean;
import st.business.tepth.business.event.GoodsEvent;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.core.util.utilcode.util.ToastUtils;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.recycler.BaseDecoration;

import static st.business.tepth.business.goodsreceive.GoodsReceiveAdapter.TYPE_LEVEL_0;


/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-02-25 10:17
 * 待领料
 */

public class GoodsReceiveDelegate extends
        MvpFragment<GoodsReceivePresenter> implements GoodsReceiveContract.View {
    public static GoodsReceiveDelegate newInstance() {
        return new GoodsReceiveDelegate();
    }

    @BindView(R2.id.tv_tile)
    TextView mTvTitle;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R2.id.check_all)
    CheckBox mCbAll;
    private GoodsReceiveAdapter mOrderAdapter;
    private DialogLoader mDialogLoader;

    @Override
    public GoodsReceivePresenter getPresenter() {
        return new GoodsReceivePresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_goods_receive;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvTitle.setText("待申请领料的物资");
        mRecyclerView.setLayoutManager(new GridLayoutManager(_mActivity, 2));
        mRecyclerView.addItemDecoration(BaseDecoration.
                create(Color.parseColor("#f0f0f0"), 1));
        mOrderAdapter = new GoodsReceiveAdapter(null,
                new GoodsReceiveAdapter.OnItemCheckAllListener() {
                    @Override
                    public void onCheckAll(boolean isCheckAll, List<MaterialWaitApplyBean> list) {
                        if (mCbAll != null) {
                            mCbAll.setChecked(isCheckAll);
                        }
                        setEnable(mTvConfirm, !list.isEmpty());
                    }
                });
        mOrderAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                MultiItemEntity entity = mOrderAdapter.getItem(position);
                if (entity != null) {
                    return entity.getItemType() == TYPE_LEVEL_0 ? 2 : 1;
                } else {
                    return 2;
                }
            }
        });
        mOrderAdapter.setEmptyView(R.layout.view_loading,
                (ViewGroup) mRecyclerView.getParent());
        mRecyclerView.setAdapter(mOrderAdapter);
        mDialogLoader = new DialogLoader(_mActivity);
        mPresenter.getData(UserManager.getInstance().getToken());
        setEnable(mTvConfirm, false);
    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        pop();
    }

    @OnClick(R2.id.check_all)
    public void onCheckAll() {
        mOrderAdapter.checkAll(mCbAll.isChecked());
        setEnable(mTvConfirm, mCbAll.isChecked());
    }

    private void setEnable(TextView tvConfirm, boolean isEnable) {
        tvConfirm.setBackgroundResource(isEnable ? R.drawable.shape_btn_operation_n :
                R.drawable.shape_btn_operation_t);
        tvConfirm.setTextColor(isEnable ? Color.parseColor("#333333") :
                Color.parseColor("#ffffff"));
        tvConfirm.setEnabled(isEnable);
    }

    @Override
    public void showData(List<MaterialWaitApplyBean> list) {
        if (mCbAll != null) {
            mCbAll.setChecked(false);
            setEnable(mTvConfirm, false);
        }
        mDialogLoader.clearDialog();
        mOrderAdapter.setEmptyView(R.layout.view_search_empty, (ViewGroup) mRecyclerView.getParent());
        mOrderAdapter.setNewData(new GoodsReceiveConvert().convertToEntity(list));
        mOrderAdapter.getLevel0Count();
    }

    @Override
    public void showError(String msg) {
        mDialogLoader.stopProgressWithFailed(msg, msg);
    }

    @Override
    public void pickSucceed() {
        EventBus.getDefault().post(new GoodsEvent());
    }

    @OnClick(R2.id.tv_confirm)
    public void applyGoods() {
        if (mOrderAdapter != null) {
            List<IDBean> idBeanList = mOrderAdapter.getSelectGoods();
            if (idBeanList != null && !idBeanList.isEmpty()) {
                mDialogLoader.showProgressDialog(true, "正在提交",
                        "请稍候...");
                mPresenter.applyGoods(UserManager.getInstance().getToken(),
                        JSON.toJSONString(idBeanList));
            } else {
                ToastUtils.showShort("请先选择物资");
            }
        }
    }
}